<template>
  <div class="region-mgr">
    <el-button type="primary" @click="openEditor()">新增</el-button>
    <el-button type="primary" @click="openSearch()">查询</el-button>
    <region-search ref="regionSearch" @onSearched="onSearch" />

    <el-table :data="regionList" border style="marginTop: 20px">
      <el-table-column align="center" type="index" width="80" label="序号" />
      <el-table-column align="center" label="区域名称" prop="name" />
      <el-table-column align="center" label="区域负责人">
        <template slot-scope="scope">
          <div>{{scope.row.list !== null ?scope.row.list.map(x => x.realname).join(','): ''}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序" prop="sort"></el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="openEditor(scope.row.id)">编辑</el-button>
          <el-button type="text" @click="remove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageIndexChange"
      :current-page.sync="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <region-editor ref="regionEditor" @refresh="loadRegionList" />
  </div>
</template>

<script>
import RegionEditor from './region/region-editor'
import RegionSearch from './region/region-search'
export default {
  name: 'region-management',
  components: {
    RegionEditor, RegionSearch
  },
  data () {
    return {
      regionList: [],
      pageIndex: 1,
      pageSize: 20,
      totalCount: 0
    }
  },
  methods: {
    pageIndexChange(val) {
      this.pageIndex = val
      this.loadRegionList()
    },
    pageSizeChange(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.pageIndexChange(1)
    },
    async loadRegionList (searchForm = {}) {
      var requestData = Object.assign(searchForm, {
        page: this.pageIndex,
        limit: this.pageSize
      })
      var res = await this.$http.post('/device/area/page', requestData)

      if (res.code === 200) {
        this.regionList = res.data.list
        this.totalCount = res.data.totalCount
      }
    },
    onSearch (searchForm) {
      this.pageIndex = 1
      this.totalCount = 0
      this.loadRegionList(searchForm)
    },
    openSearch () {
      this.$refs.regionSearch.open()
    },
    openEditor (id) {
      this.$refs.regionEditor.open(id)
    },
    async remove (id) {
      var confirm = await this.$confirm('您确认删除这个区域吗')
      if (!confirm) return
      var res = await this.$http.post('/device/area/delete', {
        id: id.toString()
      })

      if (res.code === 200) {
        this.loadRegionList()
      } else {
        this.$notify.error({title: '错误', message: res.msg})
      }
    }
  },
  created () {
    this.loadRegionList()
  }
}
</script>

<style scoped>

</style>
